<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use think\facade\Session;
use app\model\Site;
use app\utils\Utils;
use think\facade\Cookie;
use app\model\User;
use app\model\Log;


class Register extends BaseController
{
    static public function index() // 页面
    {
        $email = Session::get('email', '');
        $pw = Session::get('password', '');
        $my_user_info = Utils::user($email, $pw);

        return View::fetch(Site::get("template") . '/register', [
            'img' => Site::get("img"),
            'about' => Site::get('about'),
            'title' => Site::get("title"),
            'user_name' => $my_user_info["name"],
            'user_email' => $my_user_info["email"],
            'user_img' => $my_user_info["img"],
            'user_id' => $my_user_info["id"],
            'logo' => Site::get("logo"),
            'header' => Site::get("header"),
            'footer' => Site::get("footer"),
            'footer_about' => Site::get("footer_about"),
            'footer_list' => Site::get('footer_list'),
            'navitems' => Site::get('navitems')
        ]);
    }

    static public function register() // API
    {
        $email = Request::post('email', false);
        $pw = Request::post('pw', false);
        $post_code = Request::post('code', false);
        $name = Request::post('name', false);
        $img = 'https://pic.imgdb.cn/item/660823079f345e8d03683ae4.png';
        $user = User::where('email', $email)->select()->toArray(); // 查询数据

        if (!$email || !$pw || !$name) {
            return json(['code' => 400, 'msg' => '请填写完整']);
        }

        if (mb_strlen($name) > 50) { // 长度过大
            return json(['code' => 400, 'msg' => '字数过多']);
        }

        if (User::hasName($name)) {
            return json(['code' => 400, 'msg' => '用户名已存在']);
        }

        $verify_id = Cookie::get('verify_id', false); // 读取方式错误
        if (Utils::check_again($verify_id)['code'] == 200) { // 人机交互验证通过
            if (count($user) == 0) { // 不存在
                $code = Db::connect()->table('site_code')->where('email', $email)->select()->toArray(); // 查询数据

                if (count($code) == 0) { // 不存在
                    return json(array("code" => 403, 'msg' => '验证码错误')); // 没有验证码
                } else {
                    if ($code[0]['code'] == $post_code && $code[0]['time'] + 300 > time()) { // 验证码正确
                        Db::table('site_code')->where('email', $email)->delete();
                        $ip = Utils::getIp();

                        $data = [
                            'email' => $email,
                            'password' => hash("sha256", $pw),
                            'token' => hash("sha256", $email . $pw . time()),
                            'ip' => $ip,
                            'time' => time(),
                            'risk' => 0,
                            'money' => 0.00,
                            'name' => $name,
                            'img' => $img,
                            'bcm_id' => 0

                        ];
                        $user = new User;
                        $user->save($data);
                        $userId = $user->id;

                        Db::table('site_code')->delete($email);
                        Session::set('email', $email);
                        Session::set('password', $pw);
                        Session::set('ua', Request::header('user-agent'));
                        return json(array('code' => 200, 'data' => $userId, 'msg' => '注册成功'));
                    } else {
                        return json(array("code" => 403, 'msg' => '验证码错误')); // 验证码错误
                    }
                }
            } else {
                return json(array("code" => 100, 'msg' => '用户已存在')); // 已经有该用户
            }
        } else {
            return json(array("code" => 403, 'msg' => '未通过人机交互验证'));
        }
    }
}
